package com.rc.saas.tenant.controller.fault;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.TenantBaseController;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.constant.ShiroConstants;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.fault.RcElecPileFault;
import com.rc.saas.tenant.model.fault.RcElecPileFaultRecord;
import com.rc.saas.tenant.model.tenant.TenantRole;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.fault.RcElecPileFaultRecordSearch;
import com.rc.saas.tenant.service.fault.RcElecPileFaultRecordService;
import com.rc.saas.tenant.service.fault.RcElecPileFaultService;
import com.rc.saas.tenant.service.tenant.TenantRoleService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.util.ExportUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;


/**
 * zhoushiyu
 * 站标签
 */
@Controller
@RequestMapping("pileFaultRecord")
public class RcElecPileFaultRecordController extends TenantBaseController {
    private static Logger logger = LogManager.getLogger(RcElecPileFaultRecordController.class);

    @Autowired
    private RcElecPileFaultRecordService rcElecPileFaultRecordService;


    @Autowired
    private RcElecPileFaultService rcElecPileFaultService;

    @Autowired
    private TenantRoleService tenantRoleService;

    /**
     * 跳转站标签页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        List<RcElecPileFault> pileFaults = rcElecPileFaultService.loadRcElecPileFault();
        model.addAttribute("pileFaults",pileFaults);
        return "/templates/fault/pileFaultRecord";
    }

    /**
     * 充电设备告警-返回列表
     * @param rcElecPileFaultRecordSearch
     * @return
     */
    @RequestMapping(value = "/listPileFaultRecord", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPileFault(RcElecPileFaultRecordSearch rcElecPileFaultRecordSearch) {
        //获取登陆人信息
        TenantUser user = ShiroUtils.getSessionUser();
        TenantRole role = tenantRoleService.selectByPrimaryKey(user.getTenantRoleId());
        if (null == user || null == role || "3".equals(role.getRoleType())) {
            PageInfo<RcElecPileFaultRecord> pageInfo = new PageInfo<>();
            WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
            return webPageInfo;
        }
        String dataAuth = role.getDataAuth();//数据权限 100 平台权限, 010 加盟商权限,001企业权限,111全部权限 默认111 不能为000
        String roleType = role.getRoleType();//角色类型 ( 0 超级管理员 1 普通用户 2加盟商 3企业用户) 默认1`
        rcElecPileFaultRecordSearch.setDataAuth(dataAuth);
        rcElecPileFaultRecordSearch.setRoleType(roleType);
        rcElecPileFaultRecordSearch.setBaseOperatorId(user.getOperatorId());

        PageInfo<RcElecPileFaultRecord> pageInfo = rcElecPileFaultRecordService.listPageByCondition(rcElecPileFaultRecordSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 处理故障
     *
     * @return
     */
    @RequestMapping(value = "/dealRemark", method = RequestMethod.POST)
    @ResponseBody
    public TResponse deletePileFault(Long pileFaultRecordId,String remark) {
        RcElecPileFaultRecord info = rcElecPileFaultRecordService.selectByPrimaryKey(pileFaultRecordId);
        if (info==null){
            return TResponse.FAIL("请求错误");
        }
        info.setStatus(1);
        info.setRemark(remark);
        rcElecPileFaultRecordService.updateByPrimaryKey(info);
        return TResponse.SUCCESS();
    }

    /**
     * 导出充电设备告警数据
     */
    @ResponseBody
    @RequestMapping("/exporListPileFaultRecord")
    public TResponse exporListPileFaultRecord(RcElecPileFaultRecordSearch search, HttpServletResponse response) {
        response.setHeader("Set-Cookie", "fileDownload=true; path=/");
        //导出拼接开始条数
        try {
            ExportUtils.setDefaultPaging(search);

            //获取登陆人信息
            TenantUser user = ShiroUtils.getSessionUser();
            TenantRole role = tenantRoleService.selectByPrimaryKey(user.getTenantRoleId());
            String dataAuth = role.getDataAuth();//数据权限 100 平台权限, 010 加盟商权限,001企业权限,111全部权限 默认111 不能为000
            String roleType = role.getRoleType();//角色类型 ( 0 超级管理员 1 普通用户 2加盟商 3企业用户) 默认1`
            search.setDataAuth(dataAuth);
            search.setRoleType(roleType);
            search.setBaseOperatorId(user.getOperatorId());

            PageInfo<RcElecPileFaultRecord> list = rcElecPileFaultRecordService.listPageByCondition(search);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("充电设备告警列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), RcElecPileFaultRecord.class).sheet("充电设备告警列表").doWrite(list.getList());
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("导出失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }

}
